﻿#if NET45
using System.Collections.Generic;
using System.Configuration;

namespace Longbow.Configuration
{
    /// <summary>
    /// 配置项集合
    /// </summary>
    public class LgbConfigurationElementCollection<T>
        : ConfigurationElementCollection, IEnumerable<T>
        where T : ConfigurationElement, ILgbConfigurationElement, new()
    {
        private object syncRoot;

        /// <summary>
        /// 创建新配置项
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        /// <summary>
        /// 通过配置项获取其键值
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            var item = (T)element;
            return item.Key;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual int IndexOf(T item)
        {
            return BaseIndexOf(item);
        }

        /// <summary>
        /// 
        /// </summary>
        public new object SyncRoot
        {
            get
            {
                if (syncRoot == null)
                    System.Threading.Interlocked.CompareExchange(ref syncRoot, new object(), null);
                return syncRoot;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public virtual void Insert(int index, T item)
        {
            if (!base.IsReadOnly())
                base.BaseAdd(index, item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public virtual void RemoveAt(int index)
        {
            if (!base.IsReadOnly())
                BaseRemoveAt(index);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public virtual T this[int index]
        {
            get { return (T)BaseGet(index); }
            set
            {
                if (base.IsReadOnly())
                    return;

                var item = (T)BaseGet(index);
                if (item != null)
                    item = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual new T this[string key]
        {
            get { return (T)BaseGet(key); }
            set
            {
                if (base.IsReadOnly())
                    return;

                var item = (T)BaseGet(key);
                if (item != null)
                    item = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public virtual void Add(T item)
        {
            if (!base.IsReadOnly())
                base.BaseAdd(item);
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Clear()
        {
            if (!base.IsReadOnly())
                BaseClear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual bool Contains(T item)
        {
            return BaseIndexOf(item) > -1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            return BaseGet(key) != null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public virtual void CopyTo(T[] array, int arrayIndex)
        {
            base.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual new bool IsReadOnly
        {
            get { return base.IsReadOnly(); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual bool Remove(T item)
        {
            if (base.IsReadOnly())
                return false;

            var i = Count;
            BaseRemove(BaseGetKey(BaseIndexOf(item)));
            return Count == i - 1;
        }

        #region IEnumerable<T> Members
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public new IEnumerator<T> GetEnumerator()
        {
            for (var i = 0; i < Count; i++)
                yield return this[i];
        }
        #endregion
    }
}
#endif
